package internal

import (
	"doudizhu/msg"
	"github.com/name5566/leaf/gate"
	"reflect"
)

func handleMsg(m interface{}, h interface{}) {
	skeleton.RegisterChanRPC(reflect.TypeOf(m), h)
}

func init() {
	handleMsg(&msg.LoginMsg{}, handleLogin)
}

func handleLogin(args []interface{}) {
	m := args[0].(*msg.LoginMsg)
	a := args[1].(gate.Agent)
	ar := msg.GetAllRooms()
	if !ar.CheckPlayerExist(m.UserName) {
		a.WriteMsg("用户已存在！")
		return
	}
	ap := msg.GetAllPlayers()
	err := ap.UserLogin(m.UserName)
	if err != nil {
		a.WriteMsg(err.Error())
		return
	}
	a.WriteMsg("success")
}
